<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>智慧社区后台</title>
<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
<link rel="stylesheet" href="${pageContext.request.contextPath}/plugins/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/AdminLTE.min.css">
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/plugins/bootstrap-table/css/bootstrap-table.css">
<link href="${pageContext.request.contextPath}/plugins/bootstrap-select/css/bootstrap-select.min.css" rel="stylesheet" />
</head>
<style>
</style>
<body style="padding-bottom: 90px; background-color: #ECF0F5;">
	<section class="content" style="margin: 15px; background-color: white;">
		<div class="row">
			<div class="col-sm-12">
				<table id="table">
					<thead style="background-color: #00C0EF">
						<tr style="background-color: #00C0EF">
							<th class="text-center" data-field=""></th>
							<th class="text-center" data-field="systemMessageTitle">消息标题</th>
							<th class="text-center" data-field="people">操作人</th>
							<th class="text-center" data-field="s1">接收人</th>
							<th class="text-center" data-field="createTime">创建时间</th>
							<th class="text-center" data-field="id">操作</th>
						</tr>
					</thead>
				</table>
			</div>
		</div>
	</section>
	<!-- Large modal -->
	<div id="modal" class="modal fade bs-example-modal-lg" role="dialog">
		<div class="modal-dialog modal-lg" role="document">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal" aria-label="Close">
						<span aria-hidden="true">&times;</span>
					</button>
					<h4 class="modal-title">编辑</h4>
				</div>
				<div class="modal-body">
					<div class="form-group">
						<label for="system_message_title" class="control-label">消息标题:</label> <input type="text" class="form-control" id="system_message_title">
					</div>
					<div class="form-group">
						<label for="people" class="control-label">操作人:</label> <input type="text" class="form-control" id="people" readonly="readonly">
					</div>
					<div class="form-group" style="display: block" id="userDiv">
						<label for="user_id" class="control-label">接收人:</label> <select id="user_id" class="selectpicker form-control" multiple data-live-search="true" data-live-search-placeholder="搜索" data-actions-box="true">
						</select>
					</div>
					<br style="display: none;" id="jj2" />
					<div class="input-group">
						<span class="input-group-addon">内容</span>
						<textarea style="width: 100%; height: 500px;" class="form-control" rows="30" id="column1_textarea" name="kindEditor_content"></textarea>
					</div>
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
					<button type="button" class="btn btn-primary" id="submitUser" onclick="submitUser();">提交</button>
				</div>
			</div>
		</div>
	</div>
</body>
<script src="${pageContext.request.contextPath}/plugins/jQuery/jquery-2.2.3.min.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap/js/bootstrap.min.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/bootstrap-table.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/bootstrap-table-zh-CN.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/bootstrap-table-export.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/tableExport.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/bootstrap-table-toolbar.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/bootstrap-table-filter-control.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-select/js/bootstrap-select.min.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-select/js/i18n/defaults-zh_CN.min.js"></script>
<!-- kindeditor 编辑器 -->
<script charset="utf-8" src="${pageContext.request.contextPath}/kindeditor-4.1.10/kindeditor.js"></script>
<script charset="utf-8" src="${pageContext.request.contextPath}/kindeditor-4.1.10/lang/zh_CN.js"></script>
<!-- layer -->
<script src="${pageContext.request.contextPath}/plugins/layer/2.4/layer.js"></script>
<script src="${pageContext.request.contextPath}/js/date.js"></script>
<script>
	var smId = "";
	var editor;

	var options = {
		items : [ 'source', '|', 'undo', 'redo', '|', 'preview', 'print', 'template', 'code', 'cut', 'copy', 'paste', 'plainpaste', 'wordpaste', '|', 'justifyleft', 'justifycenter', 'justifyright',
				'justifyfull', 'insertorderedlist', 'insertunorderedlist', 'indent', 'outdent', 'subscript', 'superscript', 'clearhtml', 'quickformat', 'selectall', '|', 'fullscreen', '/',
				'formatblock', 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline', 'strikethrough', 'lineheight', 'removeformat', '|', 'image', 'multiimage',
				'flash', 'media', 'insertfile', 'table', 'hr', 'emoticons', 'baidumap', 'pagebreak', 'anchor', 'link', 'unlink', '|', 'about' ], //配置kindeditor编辑器的工具栏菜单项
		cssPath : 'kindeditor-4.1.10/plugins/code/prettify.css',
		allowFileManager : true,
		uploadJson : '${pageContext.request.contextPath}/upload_img.do', //上传
		extraFileUploadParams : {
			mienId : ''
		},
		filterMode : true, //是否启用过滤模式(编辑文本框中是否可以出现控件显示)
		htmlTags : {
			span : [ '.color', '.background-color', '.font-size', '.font-family', '.background', '.font-weight', '.font-style', '.text-decoration', '.vertical-align', '.line-height' ],
			font : [ 'id', 'class', 'color', 'size', 'face', '.background-color' ],
			a : [ 'id', 'class', 'href', 'target', 'name' ],
			embed : [ 'id', 'class', 'src', 'width', 'height', 'type', 'loop', 'autostart', 'quality', '.width', '.height', 'align', 'allowscriptaccess' ],
			img : [ 'id', 'class', 'src', 'width', 'height', 'border', 'alt', 'title', 'align', '.width', '.height', '.border' ],
			'p,ol,ul,li,blockquote,h1,h2,h3,h4,h5,h6' : [ 'id', 'class', 'align', '.text-align', '.color', '.background-color', '.font-size', '.font-family', '.background', '.font-weight',
					'.font-style', '.text-decoration', '.vertical-align', '.text-indent', '.margin-left' ],
			pre : [ 'id', 'class' ],
			hr : [ 'id', 'class', '.page-break-after' ],
			'br,tbody,tr,strong,b,sub,sup,em,i,u,strike,s,del' : [ 'id', 'class' ],
			iframe : [ 'id', 'class', 'src', 'frameborder', 'width', 'height', '.width', '.height' ]
		},
		allowFileManager : true,
		allowUpload : true, //允许上传图片
		imageUploadJson : '/saveImg', //服务端上传图片处理URI
		resizeType : 0,//使其不可以拖动 
		afterCreate : function() {
			/* var self = this;
			K.ctrl(document, 13, function() {
				self.sync();
			});
			K.ctrl(self.edit.doc, 13, function() {
				self.sync();
			}); */
		},
		afterBlur : function() {
			this.sync();
		}
	}

	KindEditor.ready(function(K) {
		editor = K.create('textarea[name="kindEditor_content"]', options);
	});

	function actionFormatter(value, row, index) {
		return [ "<button class='btn btn-primary btn-xs glyphicon glyphicon-pencil' onclick='showModal(\"" + row.smId + "\");'>查看</button>",
				"&nbsp;<button class='btn btn-danger btn-xs glyphicon glyphicon-trash' onclick='delUser(\"" + row.smId + "\")'>删除</button>", ].join('');
	}

	var table_dataset = [ {
		field : 'id',
		checkbox : true,
	}, {
		field : 'systemMessageTitle',
		align : 'center',
	}, {
		field : 'people',
		align : 'center',
	}, {
		field : 's1',
		align : 'center',
	}, {
		field : 'createTime',
		align : 'center',
		formatter : function createTimeFormatter(value, row, index) {
			return (new Date(row.createTime)).Format("yyyy-MM-dd hh:mm:ss");
		}
	}, {
		align : 'center',
		width : 200,
		formatter : actionFormatter,
	} ];
	$('#table').bootstrapTable({
		url : "${pageContext.request.contextPath}/systemMessage/selectSystemMessagePage",
		idField : "id",
		columns : table_dataset,
		striped : true, //是否显示行间隔色
		pagination : true, //是否显示分页
		pageSize : 10, //每页的记录行数
		pageList : [ 10, 25, 50, 100 ], //可供选择的每页的行数
		search : false, //是否显示搜索
		showRefresh : true,
		sidePagination : "server", //表示服务端请求  
		queryParamsType : "limit",
		queryParams : function queryParams(params) { //设置查询参数  
			var param = {
				offset : params.offset,
				limit : params.limit
			};
			return param;
		},
		toolbar : "<button class='btn btn-primary glyphicon glyphicon-plus' onclick='showModal(\"" + '' + "\");'>新增数据</button>",
		// 						+"&nbsp;<button class='btn btn-danger glyphicon glyphicon-trash'>批量删除</button>",
		icons : {
			paginationSwitchDown : 'glyphicon-collapse-down icon-chevron-down',
			paginationSwitchUp : 'glyphicon-collapse-up icon-chevron-up',
			refresh : 'glyphicon-refresh icon-refresh',
			toggle : 'glyphicon-list-alt icon-list-alt',
			columns : 'glyphicon-th icon-th',
			detailOpen : 'glyphicon-plus icon-plus',
			detailClose : 'glyphicon-minus icon-minus',
			"export" : 'glyphicon-export icon-share'
		},
		showExport : true,
		exportDataType : 'basic',
		exportTypes : [ 'json', 'xml', 'csv', 'txt', 'sql', 'excel' ]
	});

	$(function() {
		$.ajax({
			url : "${pageContext.request.contextPath}/selectAllAppUser",
			type : "get",
			dataType : "json",
			success : function(data) {
				var list = data.list;
				var user = "";
				for (var i = 0; i < list.length; i++) {
					user += "<option value='"+list[i].userId+"'>" + list[i].userName + "</option>";
				}
				$("#user_id").html(user);
				$('.selectpicker').selectpicker('refresh');

			},
			error : function() {
			}
		});
	});

	function showModal(id) {
		$('#userDiv').show();
		editor.html("");
		$('#user_id').selectpicker('deselectAll');
		if (id == "") {
			smId = '';
			$("#system_message_title").val('');
			$("#people").val("${sessionScope.userAccount}");
			$("#submitUser").show();
			$('#system_message_title').attr("disabled",false);
			editor.readonly(false);
			$('#modal').modal({
				backdrop : 'static',
				keyboard : false
			});
		} else {
			smId = id;
			$("#submitUser").hide();
			$('#system_message_title').attr("disabled",true);
			editor.readonly(true);
			$.ajax({
				url : "${pageContext.request.contextPath}/systemMessage/selectSystemMessageById",
				type : "get",
				data : {
					smId : smId
				},
				dataType : "json",
				success : function(data) {
					var systemMessage = data.systemMessage;
					$('#userDiv').hide();
					$("#system_message_title").val(systemMessage.systemMessageTitle);
					$("#people").val(systemMessage.people);
					editor.insertHtml(systemMessage.systemMessageContent);
					// 					$('#user_id').selectpicker('val', systemMessage.s2);
					$('#modal').modal({
						backdrop : 'static',
						keyboard : false
					});
				},
				error : function() {
					layer.msg('系统异常!', {
						icon : 2,
						time : 800
					});
				}
			});
		}
	}

	function submitUser() {
		var ids = $("#user_id").val();
		var s1 = $("#userDiv button").attr("title");
		//console.log($("#userDiv button").attr("title"));
		var user_ids;
		if (ids == null) {
			user_ids = "";
		} else {
			user_ids = ids.join(",");
		}
		console.log(user_ids);
		var systemMessageTitle = $("#system_message_title").val();
		var people = $('#people').val();
		var systemMessageContent = $("#column1_textarea").val();
		if (systemMessageTitle === '' || systemMessageContent === '' || editor.isEmpty()) {
			layer.alert("新增的数据中填写不完整!");
			return false;
		}

		if (smId == '') {
			if (ids == null) {
				layer.alert("新增的数据中填写不完整!");
				return false;
			}
		} else {
			s1 = '';
		}

		var url = smId === '' ? '${pageContext.request.contextPath}/systemMessage/insertSystemMessage' : '${pageContext.request.contextPath}/systemMessage/updateSystemMessage';
		$.ajax({
			url : url,
			type : "post",
			data : {
				smId : smId,
				systemMessageTitle : systemMessageTitle,
				people : people,
				systemMessageContent : systemMessageContent,
				user_ids : user_ids,
				s1 : s1
			},
			dataType : "json",
			success : function(data) {
				var result = data['result'];
				if (result === 1) {
					layer.msg('操作成功!', {
						icon : 1,
						time : 800
					});
					setTimeout(function() {
						$("#table").bootstrapTable('refresh');
						$("#modal").modal("hide");
					}, 1000);
				} else {
					layer.msg('操作失败!', {
						icon : 2,
						time : 800
					});
				}
			},
			error : function() {
				layer.msg('系统繁忙，请稍后再试!', {
					icon : 2,
					time : 800
				});
			}
		});
	}

	function delUser(id) {
		layer.confirm("是否要删除数据？", function(index) {
			$.ajax({
				url : "${pageContext.request.contextPath}/systemMessage/deleteSystemMessage",
				type : "post",
				data : {
					smId : id
				},
				dataType : "json",
				success : function(data) {
					if (data.result === 1) {
						layer.msg('操作成功!', {
							icon : 1,
							time : 800
						});
						setTimeout(function() {
							$("#table").bootstrapTable('refresh');
						}, 500);
					} else {
						layer.msg('操作失败!', {
							icon : 2,
							time : 800
						});
					}
				},
				error : function() {
					layer.msg('系统繁忙，请稍后再试!', {
						icon : 5,
						time : 800
					});
				}
			});
		});
	}
</script>
</html>
